package proxies.classes.activetransfers
{
	import core.events.PutIoErrorEvent;
	import core.events.PutIoEvent;
	import core.events.PutIoResultEvent;
	
	import flash.events.EventDispatcher;
	import flash.net.URLVariables;
	
	import proxies.Proxy;
	import proxies.classes.activetransfers.events.ActiveTransfersAddResponse;
	import proxies.classes.activetransfers.events.ActiveTransfersError;
	import proxies.classes.activetransfers.events.ActiveTransfersListResponse;
	import proxies.classes.activetransfers.events.ActiveTransfersResult;
	
	public class ActiveTransfersProxy extends Proxy implements IActiveTransfersProxy
	{		
		public static const NAME:String = "transfers";
		
		public static const METHOD_LIST:String = 'list';
		public static const METHOD_CANCEL:String = 'cancel';
		public static const METHOD_ADD:String = 'add';		
		
		public function ActiveTransfersProxy()
		{
			super(NAME);
		}
		
		public function listActiveTransfers():void
		{
			sendRequest(METHOD_LIST, onListResult, onError);			
		}

		public function addActiveTransfer(url:Array):void
		{		
			var parameters:Object = new Object();
			parameters.links = url;
			
			sendRequest(METHOD_ADD, onAddResult, onError, parameters);
		}
		
		public function cancelActiveTransfer(id:String):void
		{
			var parameters:Object = new Object();
			parameters.id = id;			
			
			sendRequest(METHOD_CANCEL, onCancelResult, onError, parameters); 
		}
		
		/**
		 * Function called if list method return an error
		 * 
		 * @param _error Error returned
		 */			
		private function onError(error:PutIoErrorEvent):void
		{
			//Add error message
			var activeTransfersError:ActiveTransfersError = new ActiveTransfersError(error.errorMessage, error.bubbles, error.cancelable);
			
			//Fire event
			dispatchEvent(activeTransfersError);
		}	
		
		/**
		 * Function called if list method return a correct result
		 * 
		 * @param _result Result returned
		 */
		private function onListResult(result:PutIoResultEvent):void
		{
			var activeTransfers:Array = buildItemList(result);
			
			//Add item list to event object
			var activeTransfersListResponse:ActiveTransfersListResponse = new ActiveTransfersListResponse(activeTransfers, result.bubbles, result.cancelable);
			
			//Fire event
			dispatchEvent(activeTransfersListResponse);
		}	
		
		private function onAddResult(result:PutIoResultEvent):void
		{			
			// Event2Array
			var activeTransfers:Array = buildItemList(result);
			
			//Add item list to event object
			var activeTransfersAddResponse:ActiveTransfersAddResponse = new ActiveTransfersAddResponse(activeTransfers,result.bubbles, result.cancelable);
			
			//Fire event
			dispatchEvent(activeTransfersAddResponse);
		}
		
		private function onCancelResult(result:PutIoResultEvent):void
		{
			
		}
		
		/**
		 * Unpack the data from the event and return a data Array.
		 */
		private function buildItemList(result:PutIoResultEvent):Array
		{
			var activeTransfers:Array = new Array();
			var currentItem:ActiveTransfersItem;
			
			for(var i:int=0; i < result.nbResults; i++)
			{		
				currentItem = new ActiveTransfersItem(result.results[i]);
				activeTransfers.push(currentItem);
			}
			
			return activeTransfers;
		}
	}
}